var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    mine: false,
    userInfo: {},
    creater: {},
    managerList: [],
    signList: []
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toggle("id", options.id);

  },
  getListData: function() {
    var that = this;
    var data = that.data;
    var userId = data.userInfo.id;
    app.req({
      url: "wechat/act/participation/listActParticipantByActId",
      method: "post",
      data: {
        activityId: data.id,
        isCheck: 1
      },
      success: function(res) {
        if (200 == res.code) {
          that.toggle("creater", {});
          that.toggle("managerList", []);
          that.toggle("signList", []);
          var list = res.data;
          for (var i in list) {
            var item = list[i];
            if (item.isCreate) {
              that.toggle("creater", item);
            }
            if (item.isManage) {
              data.managerList.push(item)
              that.toggle("managerList", data.managerList);
            }
            if (item.status) {
              data.signList.push(item)
              that.toggle("signList", data.signList);
            }
          }
          that.toggle("mine", that.data.creater.participantId == that.data.userInfo.id);
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },
  changStatus: function(event) {
    var that = this;
    var data = event.currentTarget.dataset;
    var item = {
      activityId: that.data.id,
      ids: [],
      type: 1
    };
    item.type = data.type;
    item.ids.push(data.id);
    app.req({
      url: "wechat/act/participation/updateParticipation",
      method: "post",
      data: item,
      success: function(res) {
        if (200 == res.code) {
          that.getListData();
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },
  deleteSign: function(event) {
    var id = event.currentTarget.dataset.id;
    var that = this;
    app.req({
      url: "wechat/act/participation/cancelSignIn",
      method: "post",
      data: {
        activityId: that.data.id,
        id: id
      },
      success: function(res) {
        if (200 == res.code) {
          that.getListData();
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },
  toPage: function(type) {
    wx.navigateTo({
      url: '/pages/helpsigninlist/helpsigninlist?type=' + type + '&id=' + this.data.id
    })
  },
  toChangeCreaterPage: function() {
    this.toPage(1);
  },
  toChangeManagerPage: function() {
    this.toPage(2);
  },
  toSignPage: function() {
    this.toPage(3);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("userInfo", userInfo);
      that.getListData();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})